const puppeteer = require('puppeteer');
const devices = puppeteer.devices
const iPhone = devices['iPhone X'];
const cheerio = require('cheerio');
const fs = require('fs');

let url = 'https://s.weibo.com/weibo';

let jsonData = [];

(async () => {
  const browser = await puppeteer.launch({
    headless: false,
    // args: ['--start-maximized'],
    slowMo: 250,
    devTools: true
  });
  const page = await browser.newPage();
  // await page.emulate(iPhone);
  await page.goto(url);
  await page.type('.searchbox input', '桑黄', {
    delay: 100
  });
  await page.click('.s-btn-b');
  await page.waitFor(20000); // 这个时间内手动登录
  console.log('等待完毕，开始执行')

  const target = await browser.waitForTarget(t => t.url().includes(url));
  const newPage = await target.page();
  const aHandle = await newPage.evaluateHandle(() => document.body);
  const resultHandle = await newPage.evaluateHandle(body => body.innerHTML, aHandle);
  await resultHandle.dispose();
  let pageIndex = 1;
  const turnPages = async (html) => {
    if (!html) {
      const activeTarget = await browser.waitForTarget(t => t.url().includes(url));
      const activePage = await activeTarget.page();
      const activeHandle = await activePage.evaluateHandle(() => document.body);
      const activeResHandle = await activePage.evaluateHandle(body => body.innerHTML, activeHandle);
      await activeResHandle.dispose();
      html = activeResHandle.toString();
    }
    let $ = cheerio.load(html);
    let resValue = await getListData($);
    jsonData = jsonData.concat(resValue);

    if ($('.m-page .next') && $('.m-page .next').text() && pageIndex <= 1) {
      await page.click('.m-page .next');
      pageIndex++;
      await page.waitFor(3500);
      turnPages();
    } else {
      console.log('jsonData', jsonData)
      fs.appendFile(`./data/weibo_sanghuang.json`, JSON.stringify(jsonData), 'utf-8', async function (err) {
        await browser.close();
      });
    }
  };

  turnPages(resultHandle.toString());

})();

const getListData = ($) => {
  return new Promise(async (resolve) => {
    let listNodes = $('#pl_feedlist_index .card-wrap');
    let data = [];
    for (let i = 0; i < listNodes.length; i++) {
      let itemData = {};
      let thisNode = $(listNodes[i]);
      itemData.title = thisNode.find('.content .info a.name').text();
      if(!itemData.title) {
        continue;
      }
      itemData.link = '';
      itemData.cover_url = thisNode.find('.avator img').attr('src');
      itemData.site_name = 'https://weibo.com';
      itemData.description = thisNode.find('.content p[style="display: none"].txt').text().trim() ? thisNode.find('.content p[style="display: none"].txt').text() : thisNode.find('.content p.txt').text();
      itemData.description = itemData.description.replace(/\\n/g, '').trim();
      itemData.tags = '桑黄';
      // 处理时间
      let publish_time = thisNode.find('.content p.from a:nth-child(1)').text().replace(/\\n/g, '').trim();
      publish_time = publish_time.replace(/年|月/g, '-');
      publish_time = publish_time.replace(/日/g, '');
      if (publish_time.indexOf('年') < 0) {
        let nowYear = new Date().getFullYear();
        publish_time = nowYear + '-' + publish_time;
      }
      itemData.publish_time = publish_time;
      // 处理图片（多图）
      itemData.images = ''
      let imageNodes = thisNode.find('.content .media-piclist ul li');
      for (let j = 0; j < imageNodes.length; j++) {
        itemData.images += $(imageNodes[j]).find('img').attr('src');
        if (j < imageNodes.length - 1) {
          itemData.images += ',';
        }
      }
      data.push(itemData);
    }
    resolve(data);
  });
}